package com.banyuan.utils;

import com.banyuan.dao.PageDao;
import com.banyuan.dao.TypesDao;
import com.banyuan.domain.PageInfo;

import javax.servlet.http.HttpServletRequest;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

public class PageInfoUtils {





    //        ("-----------分页开始----------------");

    public static String pageInfo(HttpServletRequest req, String condition, List al, PageDao pageDao) {

        String curr = req.getParameter("curr"); //当前在第几页
        String size = req.getParameter("size"); // 每一页有多少条记录
        Integer currInt = 0;
        Integer sizeInt = 0;

        if(!CommonUtils.isEmpty(curr)) {
            currInt = Integer.parseInt(curr);
        } else {
            currInt = 1;
        }

        if (!CommonUtils.isEmpty(size)) {
            sizeInt = Integer.parseInt(size);
        } else {
            sizeInt = 3;
        }

        // 查出所有符合查询条件的总数据统计值 total

        int total = 0;
        ResultSet resultSet = null;
        int pages = 0;
        try {
            resultSet = pageDao.selectCountAll(condition.toString(), al.toArray());
            resultSet.next();
            total = resultSet.getInt("count(*)");
            if (total%sizeInt == 0) {
                pages = total/sizeInt;
            } else {
                pages = total/sizeInt + 1;
            }


        } catch (
                SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            JDBCUtils.closeBy(resultSet);
        }

        String limit = "limit "+(currInt-1)*sizeInt+","+sizeInt;
        System.out.println(limit);

        PageInfo pageInfo = new PageInfo(total, currInt, sizeInt, pages);
        req.setAttribute("pageInfo", pageInfo);

        //        ("-----------分页结束----------------");

        return  limit;
    }



}
